package org.team751.commands;

import edu.wpi.first.wpilibj.PIDController;
import edu.wpi.first.wpilibj.command.PIDSubsystem;

/**
 * A command to eject a game piece from the gripper
 * @author Sam Crow
 */
public class GripperEject extends CommandBase {
    
    public GripperEject() {
        // Use requires() here to declare subsystem dependencies
        requires(gripperBottom);
        requires(gripperTop);
    }

    // Called just before this Command runs the first time
    protected void initialize() {
        System.out.println("GripperEject: initialize() called");
        gripperBottom.incrementTarget(90);
        gripperTop.incrementTarget(90);
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        System.out.println("GripperEject: exectue() called");
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return isClose(gripperBottom) && isClose(gripperTop);
    }

    // Called once after isFinished returns true
    protected void end() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    }
    
    /**
     * Check if a given controller, represented by a PIDSubsystem, has an actual value close to its target value
     * @param controller The PIDSubsystem to check
     * @return if the controller is close
     */
    private boolean isClose(PIDSubsystem controller){
        return Math.abs(Math.abs(controller.getPosition()) - Math.abs(controller.getSetpoint())) < 10;
    }
}
